package com.jxzt.util;

import java.io.IOException;
import java.sql.*;
import java.util.ResourceBundle;

public class DB {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    private static Connection connection;
    private static PreparedStatement preparedStatement;
    private static ResultSet resultSet;

    static{
       /* Properties properties = new Properties();
        try {
            properties.load( new FileInputStream("src/db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
         driver = properties.getProperty("driver");
         url = properties.getProperty("url");
         username = properties.getProperty("username");
         password = properties.getProperty("password");*/

        /*InputStream resourceAsStream = DB.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load( resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");*/

        ResourceBundle properties = ResourceBundle.getBundle("db");
       driver =  properties.getString("driver");
       url = properties.getString("url");
       username = properties.getString("username");
       password = properties.getString("password");


    }

    public static Connection getConn() throws ClassNotFoundException, IOException, SQLException {
        Class.forName(driver);
        connection = DriverManager.getConnection(url,username,password);
        return connection;
    }
//

//关闭接口

    public static void close(){
        try {
            if(resultSet!=null){
               resultSet.close();
           }
            if(preparedStatement!=null){
                preparedStatement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
